/**
 *
 */
package com.hula.onion.book.server.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description: web server(tomcat) connector 配置<br>
 * Create Date: 2017年4月12日 上午10:50:32
 *
 * @author liangbing
 * @version 1.0
 */
@Configuration
public class WebServerConfig {

    @Bean
    public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() {
        TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory();
        //配置自定义的Connector
        tomcatFactory.addConnectorCustomizers(new EasyTomcatConnectorCustomizer());

        return tomcatFactory;
    }

    /**
     * 自定义 tomcat connector
     */
    private class EasyTomcatConnectorCustomizer implements TomcatConnectorCustomizer {

        @Override
        public void customize(Connector connector) {

            Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();

            //设置最大连接数，允许最大的socket连接数
            protocol.setMaxConnections(1000);
            //设置最大线程数，connector可开的最大线程数
            protocol.setMaxThreads(1000);
            //socket 连接超时
            protocol.setConnectionTimeout(30000);

        }

    }
}
